﻿#include "Functions.h"

HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

// Functions
void setTextColor(int color)
{
	SetConsoleTextAttribute(hConsole, color);
}

string intToStr(int n)
{
     string tmp, ret;
     if(n < 0) {
          ret = "-";
          n = -n;
     }
     do {
          tmp += n % 10 + 48;
          n -= n % 10;
     }
     while(n /= 10);
     for(int i = tmp.size()-1; i >= 0; i--)
          ret += tmp[i];
     return ret;
}

int getKey()
{
    while (true)
    {
        for(int i = 8; i <= 256; i++)
        {
            if(GetAsyncKeyState(i) & 0x7FFF)
            {
                return i;
            }
        }
    }
}

void space(int count)
{
	for (int i = 0; i < count; i++)
		cout << " ";
}

void writeChars(wchar_t Char, int count)
{
	for (int i = 0; i < count; i++)
		cout << Char;
}

void writeLineOf(wchar_t Char)
{
	for (int i = 0; i < 80; i++)
		cout << Char;
}

void writeLine(short type = LINE_NORMAL)
{
	wchar_t line = ' ';

	switch(type)
	{
	case LINE_NORMAL:
		line = L'─';
		break;
	case LINE_UP:
		line = L'¯';
		break;
	case LINE_BLANK:
	default:
		break;
	}

	for (int i = 0; i < 80; i++)
		cout << line;
}

void setPos(short x, short y)
{
	COORD pos = {x, y};
	SetConsoleCursorPosition(GetStdHandle( STD_OUTPUT_HANDLE ), pos);
}

int rand(int min, int max)
{
	return (rand() % (max+1-min) + min);
}